/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Admin
 */
@Entity
@Table(name = "Professor")
@NamedQueries({
    @NamedQuery(name = "Professor.findAll", query = "SELECT s FROM Professor s"),
    @NamedQuery(name = "Professor.findByProfessorId", query = "SELECT s FROM Professor s WHERE s.id = :professorId"),
    @NamedQuery(name = "Professor.findByPerson", query = "SELECT s FROM Professor s WHERE s.person = :person")
})
public class Professor implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ProfessorId")
    private Long id;
    
    @ManyToMany(cascade = CascadeType.PERSIST)
    private List<Faculty> faculties;
    
    @ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "professors")
    private List<Subject> subjects;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "supervisor")
    private List<Subject> supervisedSubjects;
    
    @JoinColumn(name = "PersonId", referencedColumnName = "PersonId")
    @OneToOne(optional = true, cascade = CascadeType.ALL)
    private Person person;

    public Professor(){
    }
    
    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Professor)) {
            return false;
        }
        Professor other = (Professor) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public List<Faculty> getFaculties() {
        return faculties;
    }

    public void setFaculties(List<Faculty> faculties) {
        this.faculties = faculties;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

    public List<Subject> getSupervisedSubjects() {
        return supervisedSubjects;
    }
    
    public void addSupervisedSubject(Subject subject){
        if(supervisedSubjects == null){
            supervisedSubjects = new ArrayList<Subject>();
        }
        
        supervisedSubjects.add(subject);
    }
    
    public void addSubject(Subject subject){
        if(subjects == null){
            subjects = new ArrayList<Subject>();
        }
        
        subjects.add(subject);
    }

    public void setSupervisedSubjects(List<Subject> supervisedSubjects) {
        this.supervisedSubjects = supervisedSubjects;
    }

    @Override
    public String toString() {
        return person.getFirstName() + " " + person.getLastName() + " (" + id + ")";
    }
    
}
